//
//  MoreController.swift
//  FunSeeker
//
//  Created by 王泽宇 on 2017/3/27.
//  Copyright © 2017年 edu.Tongji.sse. All rights reserved.
//

import UIKit

class MoreController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    @IBOutlet weak var coreView: UIView!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var readyButton: UIButton!
    var chosenOne: IndexPath?
    
    weak open var delegate: MoreControllerDelegate?
    @IBOutlet weak var backView: UIView!
    var itemTexts = ["美食","饮品","电影院","酒店","快餐","日本料理","KTV","购物","美食","饮品","电影院","酒店","快餐","日本料理"]
    var picNames = ["food","drink","movie","hotel","fast_food","japanese","ktv","shopping","food","drink","movie","hotel","fast_food","japanese"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        coreView.layer.cornerRadius = 5;
        coreView.layer.shadowOpacity = 0.8
        coreView.layer.shadowColor = UIColor.black.cgColor
        coreView.layer.shadowOffset = CGSize(width: 2, height: 2)
        
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(quitMore))
        backView.addGestureRecognizer(recognizer)
        
        setupCollectionView()
        
        readyButton.setTitle("选择一个项目", for: .disabled)
        readyButton.isEnabled = false
        
        pageControl.numberOfPages = Int(ceil(Double(itemTexts.count)/8))
        pageControl.currentPage = 0
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setupCollectionView() {
        let layout = WzyFlowLayout()
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSize(width: 60, height: 77)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 8
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.alwaysBounceHorizontal = true
        collectionView.contentSize = CGSize(width: 800, height: 60)
        collectionView.reloadData()
    }
    
    @IBAction func clickReady(_ sender: UIButton) {
        didChoose(item: itemTexts[(chosenOne?.section)!*8+(chosenOne?.row)!])
    }
    
    func quitMore() {
        self.dismiss(animated: true, completion: nil)
    }
    
    func didChoose(item: String) {
        delegate?.getChosenItem(text: item, actName: picNames[itemTexts.index(of: item)!])
        quitMore()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return Int(ceil(Double(itemTexts.count)/8))
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        let tail = itemTexts.count - section*8
        if tail<8 {
            return tail
        }else{
            return 8
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MoreItemCell", for: indexPath) as! MoreItemCell
        
        cell.picName = picNames[indexPath.section*8 + indexPath.row]
        cell.itemText = itemTexts[indexPath.section*8 + indexPath.row]
        if let last = chosenOne {
            if indexPath.row == last.row && indexPath.section == last.section{
                cell.picName = cell.picName!+"_active"
            }
        }
        cell.updateUI()
        
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        chosenOne = indexPath
        collectionView.reloadData()
        readyButton.isEnabled = true
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
        pageControl.currentPage = page
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
